<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<script type="text/javascript" charset="utf-8">
var admin_qx_datagrid;
var admin_qx_editRow = undefined;
var admin_qx_addRow = undefined;

$(function(){

		
	  admin_qx_datagrid=$('#admin_qx_datagrid').datagrid({
		url : '${pageContext.request.contextPath}/userGroupMenuAction!dataGrid.action',
		fit : true,
		fitColumns : false,
		border : false,
		striped:true,
		rownumbers: true,//显示条数
		idField : 'id',
		sortOrder : 'asc',

		columns : [[{
					title:'编号',
					field:'id',
					width:100,
					align : 'center',
					checkbox : true,
				},{
					title : '用户组名称',
					field : 'userGroupList',
					width : 150,
					align : 'left',
					sortable:true,
					formatter:function(value){
						if(value){
							return value[0];
						}
					},
					editor:{
						type:'comboboxFk',
						options:{
							required:true,
							valueField:'id',
							textField:'userGroupName',
							url:'${pageContext.request.contextPath}/userGroupAction!comboboxInfo.action',
							editable:false,
							missingMessage:'用户组名称必填'
							
						}
					}			
				},{
					title : '菜单名称',
					field : 'menuList',
					width : 250,
					align : 'left',
					sortable:true,
					formatter:function(value){
						if(value){
							return value[0];
						}
					},
					editor:{
						type:'comboboxFk',
						options:{
							required:true,
							valueField:'id',
							textField:'text',
							url:'${pageContext.request.contextPath}/menuAction!comboboxInfo.action',
							editable:false,
							missingMessage:'菜单名称必填'
							
						}
					}			
				}]],
		
		 toolbar : [ {
				text : '增加',
				iconCls : 'icon-add',
				handler : function() {
					admin_qx_appendColumns();
				}
			},'-', {
				text : '删除',
				iconCls : 'icon-remove',
				handler : function() {
					admin_qx_del();
				}
			},'-',{
				text : '修改',
				iconCls : 'icon-edit',
				handler : function() {
					admin_qx_edit();
				}
			},'-', {
				text : '保存',
				iconCls : 'icon-save',
				handler : function() {
					if (admin_qx_editRow != undefined) {
						admin_qx_datagrid.datagrid('endEdit', admin_qx_editRow);
						admin_qx_editRow = undefined;
					}
					if (admin_qx_addRow != undefined) {
							var aaa=admin_qx_datagrid.datagrid('endEdit', admin_qx_addRow);
							admin_qx_datagrid.datagrid('getChanges', 'inserted');
						
					}
				}
			}, '-', {
				text : '取消编辑',
				iconCls : 'icon-undo',
				handler : function() {
					admin_qx_datagrid.datagrid('unselectAll');
					admin_qx_datagrid.datagrid('rejectChanges');
					admin_qx_editRow = undefined;
					admin_qx_addRow = undefined;
				}
			}, '-', {
				text : '取消选中',
				iconCls : 'icon-undo',
				handler : function() {
					admin_qx_datagrid.datagrid('unselectAll');
				}
			}],

			onAfterEdit : function(rowIndex, rowdata, changes) {
				var inserted = admin_qx_datagrid.datagrid('getChanges', 'inserted');
				var upqxted = admin_qx_datagrid.datagrid('getChanges', 'upqxted');
				if (inserted.length < 1 && upqxted.length < 1) {
					admin_qx_editRow = undefined;
					admin_qx_addRow = undefined;
					admin_qx_datagrid.datagrid('unselectAll');
					return;
				}
				var url = '';
				if (inserted.length > 0) {
					url = '${pageContext.request.contextPath}/userGroupMenuAction!add.action';
				}
				if (upqxted.length > 0) {
					url = '${pageContext.request.contextPath}/userGroupMenuAction!edit.action';
				}
				$.ajax({
					url : url,
					data : rowdata,
					dataType : 'json',
					type:'POST',
					success : function(r) {
						if (r.success) {
							admin_qx_datagrid.datagrid('acceptChanges');
							$.messager.show({
								msg : r.msg,
								title : '成功'
							});
							admin_qx_editRow = undefined;
							admin_qx_addRow = undefined;
							admin_qx_datagrid.datagrid('load');
						} else {
							admin_qx_datagrid.datagrid('rejectChanges');
							$.messager.alert('错误', r.msg, 'error');
						}
						admin_qx_datagrid.datagrid('unselectAll');
						admin_qx_editRow = undefined;
						admin_qx_addRow = undefined;
					}
				});
			},
			onRowContextMenu : function(e, rowIndex, rowqxta) {
				e.preventDefault();
				$(this).datagrid('unselectAll');
				$(this).datagrid('selectRow', rowIndex);
				$('#admin_qx_easyui-menu').menu('show', {
					center : e.pageX,
					top : e.pageY
				});
			},
			onDblClickRow : function(rowIndex, rowqxta) {
				if(admin_qx_addRow!=undefined){
					$.messager.show({
						msg : "请先保存或撤销编辑！",
						title : '提示',
					});
					return;
				}else{
					if (admin_qx_editRow != undefined) {
						admin_qx_datagrid.datagrid('endEdit', admin_qx_editRow);
					}

					if (admin_qx_editRow == undefined) {
						
						admin_qx_datagrid.datagrid('beginEdit', rowIndex);
						admin_qx_editRow = rowIndex;
						admin_qx_datagrid.datagrid('unselectAll');
					}
				}
				
			}
			
			});
			
		});
		
		function admin_qx_appendColumns(){
			
			if (admin_qx_editRow != undefined||admin_qx_addRow!=undefined) {
				$.messager.show({
					msg : "请先保存或撤销编辑！",
					title : '提示',
				});
				return;
			}
		
			else{
				admin_qx_datagrid.datagrid('unselectAll');
		
		
				var row = {};
				admin_qx_datagrid.datagrid('insertRow', {
					index : 0,
					row : row
				});
				admin_qx_addRow = 0;
				admin_qx_datagrid.datagrid('selectRow', admin_qx_addRow);
				admin_qx_datagrid.datagrid('beginEdit', admin_qx_addRow);
			}
		}

		function admin_qx_edit() {
			var rows = admin_qx_datagrid.datagrid('getSelections');
			if (rows.length == 1) {
				if(admin_qx_addRow!=undefined){
					$.messager.show({
						msg : "请先保存或撤销编辑！",
						title : '提示',
					});
					return;
				}else{
					if (admin_qx_editRow != undefined) {
						admin_qx_datagrid.datagrid('endEdit', admin_qx_editRow);
					}
					if (admin_qx_editRow == undefined) {
						
						admin_qx_editRow = admin_qx_datagrid.datagrid('getRowIndex', rows[0]);
						admin_qx_datagrid.datagrid('beginEdit', admin_qx_editRow);
						admin_qx_datagrid.datagrid('unselectAll');
					}
				}
				
			} else {
				$.messager.show({
					msg : '请选择一项进行修改！',
					title : '错误'
				});
			}
		}

		function admin_qx_del() {
			if (admin_qx_editRow != undefined||admin_qx_addRow!=undefined) {
				$.messager.show({
					msg : "请先保存或撤销编辑！",
					title : '提示',
				});
				return;
			}
			var rows = admin_qx_datagrid.datagrid('getSelections');
			var ids = [];
			if (rows.length > 0) {
				$.messager.confirm('请确认', '您要删除当前所选项目？', function(r) {
					if (r) {
						for ( var i = 0; i < rows.length; i++) {
							ids.push(rows[i].id);
						}
						$.ajax({
							url : '${pageContext.request.contextPath}/userGroupMenuAction!delete.action',
							data : {
								ids : ids.join(',')
							},
							type:'POST',
							dataType : 'json',
							success : function(r) {
								if (r.success) {
									admin_qx_datagrid.datagrid('acceptChanges');
									$.messager.show({
										msg : r.msg,
										title : '成功'
									});
									admin_qx_editRow = undefined;
									admin_qx_addRow = undefined;
									admin_qx_datagrid.datagrid('clearSelections');
									admin_qx_datagrid.datagrid('load');
								} else {
									admin_qx_datagrid.datagrid('rejectChanges');
									$.messager.alert('错误', r.msg, 'error');
								}
							}
						});
					}
				});
			} else {
				$.messager.alert('提示', '请选择要删除的记录！', 'error');
			}
		}
</script>
<div id="admin_qx_layout" class="easyui-layout" data-options="fit:true,border:false">
	<div data-options="region:'center',border:false">
		<table id="admin_qx_datagrid"></table>
	</div>
</div>